﻿using InfonavaEye.model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InfonavaEye.Forms
{
    public partial class CreateOperationForm : Form
    {
        public OperationInfo newEntity = null;

        private OperationInfo operationInfo = null;

        RadioButton[] opTypeGroup ;//手术类型
        RadioButton[] opPartGroup ;//手术部位

        public CreateOperationForm(OperationInfo operationInfo = null)
        {
            this.operationInfo = operationInfo;
            InitializeComponent();
        }

        private void CreateOperatingForm_Load(object sender, EventArgs e)
        {
            opTypeGroup = new RadioButton[] {
                this.radioOpType1,this.radioOpType2,this.radioOpType3,this.radioOpType4
            };
             opPartGroup = new RadioButton[] {
                this.radioPart1,this.radioPart2,this.radioPart3
            };

            if (this.operationInfo != null) {
                this.textPatient.Text = this.operationInfo.patient;
                this.Text = "查看手术单";
                this.lblTitle.Text = "查看手术单";
                this.btnBack.Visible = false;
                this.btnCreateOrSave.Visible = false;

                this.dateTimePicker1.Value = this.operationInfo.operatingDate;
                this.cmbRoom.Text = this.operationInfo.roomNum;
                this.cmbDoctor.Text = this.operationInfo.doctorName;
                this.textPatient.Text = this.operationInfo.patient;
                this.cmbFactory.Text = this.operationInfo.manufacturers;
                this.selectOpType(this.operationInfo.operationType);
                this.selectOpPart(this.operationInfo.operationPart);
            }
        }

        private void btnCreateOrSave_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.dateTimePicker1.Text)) {
                MessageBox.Show("请填写手术日期");
                return;
            }

            if (String.IsNullOrEmpty(this.cmbRoom.Text))
            {
                MessageBox.Show("请选择手术室号码");
                return;
            }

            if (String.IsNullOrEmpty(this.cmbDoctor.Text))
            {
                MessageBox.Show("请选择医生姓名");
                return;
            }


            if (newEntity == null) {

                newEntity = new OperationInfo()
                {
                    OperationId = Guid.NewGuid().ToString(),
                    operatingDate = this.dateTimePicker1.Value,
                    roomNum = this.cmbRoom.Text,
                    doctorName = this.cmbDoctor.Text,
                    patient = this.textPatient.Text,
                    manufacturers = this.cmbFactory.Text,
                };
                RadioButton opType = getOpType();
                if (opType!=null) {
                    newEntity.operationType = int.Parse(opType?.Tag.ToString());
                    newEntity.operationTypeName = opType?.Text.ToString();
                }

                RadioButton opPart = getOpPart();
                if (opPart!=null) {
                    newEntity.operationPart = int.Parse(opPart?.Tag.ToString());
                    newEntity.operationPartName = opPart?.Text.ToString();
                }
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void selectOpType(int value)
        {
            if (value >= 0) {
                opTypeGroup.ToList().ForEach(p => {
                    p.Checked = int.Parse(p.Tag.ToString()) == value;
                });
            }
        }

        private void selectOpPart(int value)
        {
            if (value >= 0)
            {
                opPartGroup.ToList().ForEach(p =>
                {
                    p.Checked = int.Parse(p.Tag.ToString()) == value;
                });
            }
        }

        private RadioButton getOpType() {
           
            return opTypeGroup.AsEnumerable().FirstOrDefault(p => p.Checked);
        }

        private RadioButton getOpPart() {
           
            return opPartGroup.AsEnumerable().FirstOrDefault(p => p.Checked);
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void radioOpType_CheckedChanged(object sender, EventArgs e)
        {

            RadioButton btn = (RadioButton)sender;
            var value = int.Parse(btn.Tag.ToString());
            this.selectOpType(value);

        }

        private void radioOpPart_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton btn = (RadioButton)sender;
            var value = int.Parse(btn.Tag.ToString());
            this.selectOpPart(value);
        }
    }
}
